<?php
namespace app\home\Controller;
use think\Controller;
use think\Request;
use think\Db;
// use app\index\model\index;
class Shop extends Controller
{
    // 商品列表
    public function shop_list()
    {
        header("content-type:text/html;charset=utf-8");
        // 接受分类id
        $type_c_id = input('get.c_id');
        // 父级id
        $type_c_pid = input('get.c_pid');

        // 接受搜索的商品值
        $keyword = input('post.keyword');
        $where = "";
        if(!empty($type_c_id)){
            $where = "c_id in($type_c_id)";
            // 类型表
            $sql = "select * from man_category where c_id=$type_c_id or c_id=$type_c_pid";
            $typeDataA = Db::query($sql);
        }
        $typeDataA = isset($typeDataA)?$typeDataA:'';
        if(!empty($keyword)){
            $where = "g_name like '%$keyword%'";
        }
        
        // 商品表
        if ($type_c_pid == 0 && !empty($type_c_id)) {
            $sql = "select * from man_goods where c_id in(
                select c_id from man_category where c_pid in(
                    select c_id from man_category where c_id in($type_c_id)
                    )
                )";
        }else{
            $sql = "select * from man_goods where ".$where;
        }
        $goodsData = Db::query($sql);
        // var_dump($goodsData);die;
        $typeData = $this->alltype();
        return view('shop_list',[
            'typeData'=>$typeData, //全部分类
            'typeDataA'=>$typeDataA, //当前分类
            'goodsData'=>$goodsData //当前分类下的商品
            ]
        );
    }

    // 商品详情页
    public function iemt()
    {
        header("content-type:text/html;charset=utf-8");
        // 接受商品id
        $goods_g_id = input('get.g_id');
        $type_c_id = input('get.c_id');
        // $type_c_pid = input('get.c_pid');
        // 查找分类表
        $sql = "select * from man_category where c_id=$type_c_id";
        $typeDataA = Db::query($sql);
        if(!empty($type_c_id)){
            //查询推荐的商品
            $sql = "select * from man_goods where c_id=$type_c_id";
            $goodsDataTuijian = Db::query($sql);
        }
        if(!empty($goods_g_id)){
            // 查询当前的一个商品
            $sql = "select * from man_goods where g_id=$goods_g_id";
            $goodsDataOne = Db::query($sql);
            // 查询当前用户是否收藏该商品
            $sql = "select * from man_product_sc where g_id = $goods_g_id";
            $collectOne = Db::query($sql);
            // 查询详情页小图片
            $sql = "select * from man_goodsimg where g_id=$goods_g_id and g_statc=1 order by g_sort asc";
            $goodsImg = Db::query($sql);

            // 查询属性值
            $sql = "select * from man_attr_value where g_id=$goods_g_id";
            $attr_value = Db::query($sql);
            if($attr_value){
                // 循环属性值
                foreach ($attr_value as $key=>$val){
                    // 根据属性值查对应属性
                    $sql = "select a_id,a_name from man_attr where a_id=".$val['a_id'];
                    $attr1 = Db::query($sql);
                    // 赋值
                    $attr[] = $attr1['0'];
                }
                // 将查出来的属性循环 变成一维数组
                foreach ($attr as $k=>$v){
                    $v=join(',',$v); //降维,也可以用implode,将一维数组转换为用逗号连接的字符串
                    $attr1[$k]=$v;
                }
                $attr2=array_unique($attr1); //去掉重复的字符串,也就是重复的一维数组
                foreach ($attr2 as $k => $v){
                    $array=explode(',',$v); //再将拆开的数组重新组装
                    //下面的索引根据自己的情况进行修改即可
                    $attr3[$k]['a_id'] =$array[0];
                    $attr3[$k]['a_name'] =$array[1];
                }
                // 空数组
                $array = [];
                foreach($attr3 as $key=>$val){
                    $array[$key] = $val;
                    $aaa = [];
                    foreach ($attr_value as $k=>$v){
                        if($val['a_id'] == $v['a_id']){
                            $aaa[] = $v;
                        }
                    }
                    $array[$key]['aa_id'] = $aaa;
                }
            }
                
        }
        $goodsDataOne = isset($goodsDataOne)?$goodsDataOne:'';
        $goodsImg = isset($goodsImg)?$goodsImg:'';
        $goodsDataTuijian = isset($goodsDataTuijian)?$goodsDataTuijian:'';
        $array = isset($array)?$array:'';
        $collectOne = isset($collectOne)?$collectOne:'';

        $typeData = $this->alltype();
        return view('iemt',[
            'typeData'=>$typeData, //全部分类
            'typeDataA'=>$typeDataA, //当前分类
            'goodsDataOne'=>$goodsDataOne, //当前商品
            'goodsDataTuijian'=>$goodsDataTuijian,  //推荐商品
            'goodsImg'=>$goodsImg, //当前商品图片
            'array'=>$array, //当前商品属性
            'collectOne'=>$collectOne //当前商品是否收藏
            ]
        );
    }
    // 收藏
    public function collectAdd()
    {
        $g_id = input("get.g_id");
        $sc_statc = input("get.sc_statc");

        $u_id = cookie('u_id');
        if(!empty($g_id) && isset($u_id)){
            $sql = "select * from man_product_sc where g_id=$g_id and u_id=$u_id";
            $prodectData = Db::query($sql);
            if($prodectData){
                $sql = "update man_product_sc set sc_statc='$sc_statc' where g_id=$g_id and u_id=$u_id";
                Db::query($sql);
            }else{
                $sql = "insert into man_product_sc values(null,$u_id,$g_id,$sc_statc)";
                Db::query($sql);
            }
        }
        echo "1";
    }
    // 加入购物车
    public function cartAdd()
    {
        $data = input("get.");
        var_dump($data);
    }

    // 
    public function details(){
        $g_id = input('get.g_id');
        $sql = "select a_id,a_name from man_attr_value where g_id =$g_id";
        $attr_value = Db::query($sql);
        $attr_value=$attr_value;
        // 循环赋值对应属性
        foreach ($attr_value as $key=>$val){
            $sql = "select a_id,a_name from man_attr where a_id=".$val['a_id'];
            $attr1 = Db::query($sql);
            $attr[] = $attr1['0'];
        }
        foreach ($attr as $k=>$v){
            $v=join(',',$v); //降维,也可以用implode,将一维数组转换为用逗号连接的字符串
            $temp[$k]=$v;
        }
        $attr=array_unique($temp); //去掉重复的字符串,也就是重复的一维数组
        foreach ($attr as $k => $v){
            $array=explode(',',$v); //再将拆开的数组重新组装
            //下面的索引根据自己的情况进行修改即可
            $temp2[$k]['a_id'] =$array[0];
            $temp2[$k]['a_name'] =$array[1];
        }
        $array = [];
        foreach($temp2 as $key=>$val){
            $array[$key] = $val;
            foreach ($attr_value as $k=>$v){
                if($val['a_id'] == $v['a_id']){
                    $aaa[] = $v;
                }
            }
            $array[$key]['aa_id'] = $aaa;
        }
//var_dump($array);die;


        $sql = "select * from man_goods inner join man_category on man_goods.c_id = man_category.c_id where g_id='$g_id'";
        $goods = Db::query($sql);
        $imgsql = "select * from man_goodsimg where g_id='$g_id'";
        $img = Db::query($imgsql);

        return view('details',['goods'=>$goods,'img'=>$img,'array'=>$array]);
    }

    

    // 分类查询
    public function alltype()
    {
        // 查询分类
        $sql = "select * from man_category";
        $typeData = Db::query($sql);
        $typeData = $this->createTreeBySon($typeData);
        return $typeData;
    }
    // 多级分类递归方法  多维数组
    public function createTreeBySon($info,$c_pid=0)
    {
        // 定义数组
        $newArr = array();
        // 处理
        foreach ($info as $k=>$v) {
            // 判断
            if($v['c_pid'] == $c_pid){

                // 把v赋给新数组追加一个下标
                $newArr[$k] = $v;

                // 调用自己 查找所有子辈放到son下
                $newArr[$k]['son'] = $this->createTreeBySon($info,$v['c_id']);
            }
        }
        // 数组返回
        return $newArr;
    }
}